using System.Globalization;
using UnityEngine;

namespace Game {
	public class UnitMemory : IMemory {
		const char Delimiter = '|';

		ComputingUnit _owner;

		public UnitMemory(ComputingUnit owner) {
			_owner= owner;
		}

		public void ClearAll() {
			_owner.Data.UserParams.Clear();
		}

		public bool GetBool(string key, bool defaultValue = false) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return defaultValue;
			}
			if ( _owner.Data.UserParams.TryGetValue(key, out var value) ) {
				if ( bool.TryParse(value, out var val) ) {
					return val;
				}
			}
			return defaultValue;
		}

		public float GetFloat(string key, float defaultValue = 0) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return defaultValue;
			}
			if ( _owner.Data.UserParams.TryGetValue(key, out var value) ) {
				if ( float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var val) ) {
					return val;
				}
			}
			return defaultValue;
		}

		public int GetInt(string key, int defaultValue = 0) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return defaultValue;
			}
			if ( _owner.Data.UserParams.TryGetValue(key, out var value) ) {
				if ( int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var val) ) {
					return val;
				}
			}
			return defaultValue;
		}

		public string GetString(string key, string defaultValue = null) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return defaultValue;
			}
			if ( _owner.Data.UserParams.TryGetValue(key, out var value) ) {
				return value;
			}
			return defaultValue;
		}

		public Vector3 GetVector3(string key, Vector3 defaultValue = default) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return defaultValue;
			}
			if ( _owner.Data.UserParams.TryGetValue(key, out var value) ) {
				var elems = value.Split(Delimiter);
				if ( elems.Length == 3 ) {
					float.TryParse(elems[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var x);
					float.TryParse(elems[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var y);
					float.TryParse(elems[2], NumberStyles.Any, CultureInfo.InvariantCulture, out var z);
					return new Vector3(x,y,z);
				}
			}
			return defaultValue;
		}

		public void SetBool(string key, bool value) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return;
			}
			var strVal = value.ToString(CultureInfo.InvariantCulture);
			if ( _owner.Data.UserParams.ContainsKey(key) ) {
				_owner.Data.UserParams[key] = strVal;
			} else {
				_owner.Data.UserParams.Add(key, strVal);
			}
		}

		public void SetFloat(string key, float value) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return;
			}
			var strVal = value.ToString(CultureInfo.InvariantCulture);
			if ( _owner.Data.UserParams.ContainsKey(key) ) {
				_owner.Data.UserParams[key] = strVal;
			} else {
				_owner.Data.UserParams.Add(key, strVal);
			}
		}

		public void SetInt(string key, int value) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return;
			}
			var strVal = value.ToString(CultureInfo.InvariantCulture);
			if ( _owner.Data.UserParams.ContainsKey(key) ) {
				_owner.Data.UserParams[key] = strVal;
			} else {
				_owner.Data.UserParams.Add(key, strVal);
			}
		}

		public void SetString(string key, string value) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return;
			}
			if ( _owner.Data.UserParams.ContainsKey(key) ) {
				_owner.Data.UserParams[key] = value;
			} else {
				_owner.Data.UserParams.Add(key, value);
			}
		}

		public void SetVector3(string key, Vector3 value) {
			if ( _owner == null || string.IsNullOrEmpty(key) ) {
				return;
			}
			var strVal = $"{value.x.ToString(CultureInfo.InvariantCulture)}{Delimiter}{value.y.ToString(CultureInfo.InvariantCulture)}{Delimiter}{value.z.ToString(CultureInfo.InvariantCulture)}";
			if ( _owner.Data.UserParams.ContainsKey(key) ) {
				_owner.Data.UserParams[key] = strVal;
			} else {
				_owner.Data.UserParams.Add(key, strVal);
			}
		}
	}
}
